package io.gitee.tuo_tuo.jfinaldevassistance.common.config;

import com.jfinal.config.*;
import com.jfinal.i18n.I18n;
import com.jfinal.i18n.Res;
import com.jfinal.kit.PropKit;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;
import io.gitee.tuo_tuo.jfinaldevassistance.common.model._MappingKit;
import io.gitee.tuo_tuo.jfinaldevassistance.interceptors.ControllerEnhance;
import io.gitee.tuo_tuo.jfinaldevassistance.interceptors.GlobalExceptionInterceptor;
import io.gitee.tuo_tuo.jfinaldevassistance.json.IntelligenceSelectJsonFactory;
import io.gitee.tuo_tuo.jfinaldevassistance.validator.ValidatorInterceptor;

import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;


public class DevConfig extends JFinalConfig {

	// log4j 记录日志
	private Log log = Log.getLog(DevConfig.class);

	public static Res res;

	@Override
	public void configConstant(Constants me) {
		// 加载少量必要配置，随后可用PropKit.get(...)获取值
		log.info("********开始加载配置文件********");
		PropKit.use("config.txt"); 
		log.info("********配置文件加载完成********");

		log.info("********开始加载资源文件********");
		me.setI18nDefaultLocale("zh_CN");
		me.setI18nDefaultBaseName("dev");
		me.setEncoding("utf-8");
		res = I18n.use();
		log.info("********resource file load successfully********");

		try {
			initH2DB();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}

		me.setDevMode(PropKit.getBoolean("devMode", false));

		// 设置视图类型为Jsp，否则默认为FreeMarker
		me.setViewType(ViewType.JSP);
		me.setBaseUploadPath(PropKit.use("config.txt").get("upload.base.folder"));
		me.setJsonFactory(new IntelligenceSelectJsonFactory());
	}

	@Override
	public void configEngine(Engine arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void configHandler(Handlers arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void configInterceptor(Interceptors arg0) {
		// TODO Auto-generated method stub
		arg0.add(new GlobalExceptionInterceptor());
		arg0.addGlobalActionInterceptor(new ControllerEnhance());
		arg0.addGlobalActionInterceptor(new ValidatorInterceptor());
	}

	@Override
	public void configPlugin(Plugins me) {
		// 配置数据库连接池插件
		DruidPlugin druidPlugin = createDruidPlugin();
		me.add(druidPlugin);
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		me.add(arp);

		// 所有配置在 MappingKit 中搞定
		_MappingKit.mapping(arp);
	}

	@Override
	public void configRoute(Routes route) {
		route.add(new IndexRoutes());
	}

	private void initH2DB() throws SQLException, URISyntaxException {
		String schemaSql = getClass().getResource("/DbInitSql.sql").toURI().toString().substring(6);

		String jdbcUrl = PropKit.get("jdbcUrl");
		String user = PropKit.get("user");
		String password = PropKit.get("password").trim();
		Connection conn = DriverManager.getConnection(jdbcUrl, user, password);
		Statement st = conn.createStatement();
		st.execute( "drop all objects;");// 这一句可以不要

		boolean executeResult = st.execute("runscript from '" + schemaSql + "'");
		st.close();
		conn.close();
	}

	public static C3p0Plugin createC3p0Plugin() {
		return new C3p0Plugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim(),
				PropKit.get("driverClassName").trim(), 400, 10, 20,
				40, 2);
	}

	public static DruidPlugin createDruidPlugin() {
		return new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim(), PropKit.get("driverClassName").trim());
	}

}
